﻿package {
	import flash.display.Sprite;
	import fl.transitions.Tween;
	import fl.transitions.easing.Strong;
	import fl.transitions.easing.Regular;
	import flash.display.Loader;
	import flash.net.URLRequest;
	import flash.events.*;
	import fl.transitions.*;
	import fl.transitions.TweenEvent;
	public class ZoomContainer extends Sprite {

		private var loader:Loader=new Loader();
		private var urlRequest:URLRequest;
		private var tweenW:Tween;
		private var tweenH:Tween;

		public var photoW:uint;
		public var photoH:uint;

		private function dispatch() {
			this.dispatchEvent(new Event("ZOOM_END"));
		}
		
		public function unloadPhoto() {
			loader.unload();
			//使用unload方法是不是也删除了在loader.contentLoaderInfo里注册的侦听器???
		}
		public function load_zoom(url:String) {
			this.dispatchEvent(new Event("LOAD_BEGIN"));
			urlRequest=new URLRequest(url);
			loader.load(urlRequest);
			loader.contentLoaderInfo.addEventListener(Event.INIT,initHandler);
			loader.contentLoaderInfo.addEventListener(Event.COMPLETE,completeHandler);
			loader.contentLoaderInfo.addEventListener(IOErrorEvent.IO_ERROR,errorHandler);
			function initHandler(evt:Event) {
				photoW=evt.target.width;
				photoH=evt.target.height;
				evt.target.removeEventListener(Event.INIT,initHandler);
			}
			function completeHandler(evt:Event) {
				loader.x=-Math.floor(photoW/2);
				loader.y=-Math.floor(photoH/2);
				tweenW=new Tween(getChildAt(0),"width",Regular.easeOut,getChildAt(0).width,photoW+20,0.5,true);
				tweenH=new Tween(getChildAt(0),"height",Regular.easeOut,getChildAt(0).height,photoH+20,0.5,true);
				tweenW.addEventListener(TweenEvent.MOTION_FINISH,finishHandler);
				function finishHandler(evt:Event) {
					addChildAt(loader,1);
					dispatch();
				}
				evt.target.removeEventListener(Event.COMPLETE,completeHandler);
			}
			function errorHandler(evt:Event) {
				trace("load file faild");
				evt.target.removeEventListener(IOErrorEvent.IO_ERROR,errorHandler);
			}
		}
		
		public function ZoomContainer() {
			var mainContainer:Sprite=new Sprite();
			addChildAt(mainContainer,0);
			with (mainContainer) {
				graphics.beginFill(0xffffff,1);
				graphics.drawRect(-5,-5,10,10);
				graphics.endFill();
			}
		}//end new function
	}
}